import QtQuick
import QtQuick.Controls
import Qt.labs.platform as Platform // 解决命令冲突
import QtQuick.Controls.Material // for 移动版

ApplicationWindow {
    width: 320
    height: 480
    visible: true
    title: qsTr("Image Viewer")

    background: Rectangle {
        color: 'darkGray'
    }

    Image {
        id: image
//        source: "images/qq.png" // for test
        anchors.fill: parent
        fillMode: Image.PreserveAspectFit
        asynchronous: true // 打开异步, 加载时可能更快
    }

    header: ToolBar {
        Material.background: Material.Orange
        ToolButton {
            icon.source: "images/details"
            onClicked: drawer.open()
        }
        Label {
            anchors.centerIn: parent
            text: 'Image Viewer'
            font.pixelSize: 20
        }
    }

    Platform.FileDialog {
        // folder: // 控制默认选择的目录
        id: fileOpenDialog
        title: "选择图片"
        nameFilters: [
            "Images files(*.png *.jpg)", "all files(*.*)"
        ]

        onAccepted: {
            image.source = fileOpenDialog.file
        }
    }

    Drawer {
        id: drawer

        width: parent.width * 2 / 3
        height: parent.height

        ListView {
            anchors.fill: parent
            model: ListModel {
                ListElement {
                    text: 'open...'
                    triggered: function() {
                        fileOpenDialog.open();
                    }
                }
                ListElement {
                    text: 'About...'
                    triggered: function() {
                        aboutDialog.open();
                    }
                }
            }

            delegate: ItemDelegate {
                text: model.text
                highlighted: ListView.isCurrentItem
                onClicked: {
                    drawer.close()
                    model.triggered()
                }
            }
        }

        Dialog {
            id: aboutDialog
            width: 300; height: 150
            anchors.centerIn: parent
            title: "About"
            Label {
                anchors.fill: parent
                text: "AAAAAAAAAA\nBBBBBBBBBB"
                horizontalAlignment: Text.AlignHCenter
            }
            standardButtons: Platform.StandardButton.Ok
        }
    }
}
